import requests
import re
from lxml import etree

cookies = {
    '__zse_ck': '004_VuGfqJTlKiJOO20tAO1srVc5SW=LO/1c/SDxeep=BkqIJ5jkV9O9Tpjd9t4alwK8nGyllxtNbzH9CaidmsoSs6OVvGOkKG3r31cUz5QqfVKyfdaKmSUERSthxP2YmifB-aST4Ms0FlzSdn7atWPiBdQAUKDfjhfaO/jMYTy7Z36Od8UDBH69ZjDoaJrNRQrcpP5H3cgaXHfVV44hrl6sfxOEAOXMxkObp/kxoAN8Ng4GhGRGicyAW1Ze5/LWYVvlE',
    'z_c0': '2|1:0|10:1761911574|4:z_c0|92:Mi4xX2owRGtBQUFBQUNRRWxSWUNwTk9HeVlBQUFCZ0FsVk5Gdkh4YVFDNkxpWkViNmUzVTdiM0UwdWVFSkZyY0VpUkFn|a3547c7e823d592d62d1e25d8664915908b0adca14e294bfc80485553f44c28a',
}

headers = {

    'referer': 'https://www.zhihu.com/signin?next=%2Fhot',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
}
hot_datas = []
response = requests.get('https://www.zhihu.com/hot', cookies=cookies, headers=headers)
html = response.text
tree = etree.HTML(html)
hot_lists = tree.xpath('//div[@class="HotItem-content"]')
for hot_list in hot_lists:
    title = hot_list.xpath('.//h2[@class="HotItem-title"]')[0].text
    detail_url = hot_list.xpath(f'.//a[@title="{title}"]/@href')[0]
    img_url = hot_list.xpath(f'//img[@alt="{title}"]/@src')[0]
    tiezi_haoma = detail_url.split('/')[-1]
    tiezi_url = f'https://www.zhihu.com/api/v4/questions/{tiezi_haoma}/feeds?offset=1'
    response = requests.get(tiezi_url, cookies=cookies, headers=headers)
    datas = response.json()
    tiezi = []
    for item in datas['data']:
        content = item['target']['excerpt']
        name = item['target']['author']['name']
        tiezi.append({
            'name': name,
            'content': content
        })
    hot_datas.append({
        'title': title,
        'detail_url': detail_url,
        'img_url': img_url,
        'tiezi': tiezi,
    })
print(hot_datas)
